package dinner.web.money;

import java.io.File;
import java.util.Calendar;
import java.util.List;

import dinner.web.eat.IO;
import dinner.web.libs.Cashe;
import dinner.web.libs.IOUtils;

public class MoneyIO {

	private static Cashe<String, List<MoneyRecord>> data;
	private static int timestamp;

	static List<MoneyRecord> getUserStat(String name) {
		List<MoneyRecord> record = checkCashedData(name);
		if (record == null) {
			record = IOUtils.read(new File(IO.getDBForCurrentDate(), "money_" + name + ".dba"), MoneyRecord.class);
		}
		// TODO
		return record;
	}

	private static synchronized List<MoneyRecord> checkCashedData(String name) {
		if (Calendar.getInstance().get(Calendar.DAY_OF_YEAR) == timestamp)
			return data.getCachedData(name);
		else {
			data.clearAll();
			timestamp = Calendar.getInstance().get(Calendar.DAY_OF_YEAR);
			return null;
		}
	}

	synchronized static void addToUserStat(String name, double summ) {
		// TODO
	}

}
